<?php
class product extends CI_Controller
{
	  
	protected $_templates;
	
	public function __construct()
	{
        parent::__construct();
		
		$this->load->model('product_model','product');
		$this->load->model('news_model','news');
		$this->load->helper('img_helper');
		$this->load->helper('MY_number');
		$this->load->helper('str');
		$this->load->helper('img');
		//$this->load->config('config_menu');
		//$this->load->config('config_ismainmenu');
		$this->pre_message = "";
		$this->permit_library->check_permit();
		$this->session->set_userdata(array('Url'=>uri_string()));           
	}
	
	// Thống kê toàn bộ gian hàng
	function index()
	{
		$data['title'] = 'Thống kê';
		$this->_templates['page'] = 'product/static';
		$this->site->load($this->_templates['page'],$data);
	}
      
	function listcat()
	{
		$data['title'] = 'Danh sách danh mục sản phẩm';
		$data['add'] = 'product/addcat';
		$cid = (int)$this->uri->segment(3);
		$config['base_url'] = base_url().'product/listcat/'.$cid;  
		$config['total_rows']   =  $this->product->getNumCat($cid);
		$data['num'] = $config['total_rows'];
		$config['per_page']  =   20;
		$config['uri_segment'] = 4; 
		$this->pagination->initialize($config);   
		
		//$this->create_menu();
		//$this->create_ismainmenu();
		
		$data['list'] =   $this->product->getAllCat($cid,$config['per_page'],$this->uri->segment('4'));
		$data['pagination']    	= $this->pagination->create_links();   
		$data['message']        = $this->pre_message;
		$this->_templates['page'] = 'product/cat/listcat';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function create_menu()
	{
		$str = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n/**\n* Cache file for config_menu quagiare.com.\n* Date: ".date('d/m/y H:i:s').".\n**/";
		
		$listcat_cap1 = $this->product->getMaincat(); 
		$c1=1;
		$total_menu =count($listcat_cap1);
		$str .= "\n";
		$str .= "\n\$config['total_menu'] = $total_menu;"; 
		$str .= "\n";  
		          
		foreach($listcat_cap1 as $cap1):
			$str .="/*********Cấp 1_".$c1."**************/";
			$cap1_id = $cap1->CategoryID;
			$cap1_name = str_replace("'","\'",$cap1->CategoryName);
			$cap1_alias = str_replace("'","\'",$cap1->CategoryAlias);
			
			$str .= "\n\$config['menu_id_leve_1_$c1'] = $cap1_id;";   
			$str .= "\n\$config['menu_name_leve_1_$c1'] = '$cap1_name';";   
			$str .= "\n\$config['menu_alias_leve_1_$c1'] = '$cap1_alias';";   
			
			$listcat_cap2 = $this->product->check_parentid($cap1->CategoryID);
			$total_cap2 = count($listcat_cap2);
			$str .= "\n\$config['menu_total_leve_1_$c1'] = $total_cap2;";
			
			if($total_cap2 > 0)
			{
				$c2 = 1;
				foreach($listcat_cap2 as $cap2):
					$str .= "\n";
					$str .="\n/*********Cấp 2_".$c1."_".$c2."**************/";
					$cap2_id = $cap2->CategoryID;
					$cap2_name = str_replace("'","\'",$cap2->CategoryName);
					$cap2_alias = str_replace("'","\'",$cap2->CategoryAlias);
					
					$str .= "\n\$config['menu_id_leve_2_".$c1."_".$c2."'] = $cap2_id;";   
					$str .= "\n\$config['menu_name_leve_2_".$c1."_".$c2."'] = '$cap2_name';"; 
					$str .= "\n\$config['menu_alias_leve_2_".$c1."_".$c2."'] = '$cap2_alias';"; 
					   
					$listcat_cap3 = $this->product->check_parentid($cap2->CategoryID);
					$total_cap3 = count($listcat_cap3);
					$str .= "\n\$config['menu_total_leve_2_".$c1."_".$c2."'] = $total_cap3;";
					if($total_cap3 > 0)
					{
						$c3 = 1;
						foreach($listcat_cap3 as $cap3):
							$str .= "\n";
							$str .="\n/*********Cấp 3_".$c1."_".$c2."_".$c3."**************/";                            
							$cap3_id = $cap3->CategoryID;
							$cap3_name = str_replace("'","\'",$cap3->CategoryName);
							$cap3_alias = str_replace("'","\'",$cap3->CategoryAlias);
							
							$str .= "\n\$config['menu_id_leve_3_".$c1."_".$c2."_".$c3."'] = $cap3_id;";   
							$str .= "\n\$config['menu_name_leve_3_".$c1."_".$c2."_".$c3."'] = '$cap3_name';";    
							$str .= "\n\$config['menu_alias_leve_3_".$c1."_".$c2."_".$c3."'] = '$cap3_alias';";    
							
							$listcat_cap4 = $this->product->check_parentid($cap3->CategoryID);
							$total_cap4 = count($listcat_cap4);
							$str .= "\n\$config['menu_total_leve_3_".$c1."_".$c2."_".$c3."'] = $total_cap4;";
							if($total_cap4 > 0)
							{
								$c4 = 1;
								foreach($listcat_cap4 as $cap4):
									$str .= "\n";
									$str .="\n/*********Cấp 4_".$c1."_".$c2."_".$c3."_".$c4."**************/";                            
									$cap4_id = $cap4->CategoryID;
									$cap4_name = str_replace("'","\'",$cap4->CategoryName);
									$cap4_alias = str_replace("'","\'",$cap4->CategoryAlias);
									
									$str .= "\n\$config['menu_id_leve_4_".$c1."_".$c2."_".$c3."_".$c4."'] = $cap4_id;";   
									$str .= "\n\$config['menu_name_leve_4_".$c1."_".$c2."_".$c3."_".$c4."'] = '$cap4_name';";    
									$str .= "\n\$config['menu_alias_leve_4_".$c1."_".$c2."_".$c3."_".$c4."'] = '$cap4_alias';";    
									
								$c4++;                   
								endforeach;  
							}   
							$str .= "\n";
							
						$c3++;                   
						endforeach;  
					}   
					$str .= "\n";                     
				$c2++;                   
				endforeach;  
			}
			$str .= "\n";
			$str .= "\n";
		$c1++;
		endforeach;
		$str .= "\n";
		$str .= "\n";
		/*$mentintuc = $this->news->get_Parent(0);
		$total_news_cat = count($mentintuc);
		$str .= "\n\$config['news_total'] = $total_news_cat;";
		$m = 1;
		foreach($mentintuc as $val):
			$newsub = $this->news->get_Parent($val->CatID);
			
			
			$cat_id = $val->CatID;
			$cat_name  = str_replace("'","\'",$val->CatName);
			$cat_alias = str_replace("'","\'",$val->Alias);
			$str .= "\n\$config['news_id_".$m."'] = $cat_id;"; 
			$str .= "\n\$config['news_alias_".$m."'] = '$cat_alias';"; 
			$str .= "\n\$config['news_name_".$m."'] = '$cat_name';"; 
			$str .= "\n";
		  
			$total_news_sub = count($newsub);
			if($total_news_sub > 0){
				$str .= "\n\$config['news_total_".$m."'] = $total_news_sub;";
				$j=1;
				foreach($newsub as $val1):
					$cat_id_sub = $val1->CatID;
					$cat_alias_sub  = str_replace("'","\'",$val1->Alias);
					$cat_name_sub = str_replace("'","\'",$val1->CatName);
					$str .= "\n\$config['news_id_".$m."_".$j."'] = $cat_id_sub;"; 
					$str .= "\n\$config['news_alias_".$m."_".$j."'] = '$cat_alias_sub';"; 
					$str .= "\n\$config['news_name_".$m."_".$j."'] = '$cat_name_sub';";                         
					$str .= "\n";  
				
				$j++;
				endforeach;
			}
		   $m++;  
		endforeach;*/
		$str .= "\n\n/* End of file config_menu quagiare.com*/\n/* Location: ./cf/config_menu.php */";        
		write_file('../cf/config_menu.php', $str);
	}
	
	function create_ismainmenu()
	{
	  
		$str = "<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');\n/**\n* Cache file for config_ismainmenu quagiare.com.\n* Date: ".date('d/m/y H:i:s').".\n**/";
		
		$c1=1;
		$listcat_cap1 = $this->product->get_all_is_mainmenu();
		$total_menu =count($listcat_cap1);
		/*$list_ads_daugia = $this->product->get_ads_by_cat(9999);
		$str .= "\n";
		$str .= "\n\$config['total_main'] = $total_menu;"; ;
		$str .= "\n";     
		$i=1;
		foreach($list_ads_daugia as $rs):
			$ads_name = str_replace("'","\'",$rs->AdsName);
			$ads_link = str_replace("'","\'",$rs->AdsLink);
			$ads_img = $rs->AdsImg;
			$str .= "\n\$config['ads_daugia_name_".$i."'] = '$ads_name';"; 
			$str .= "\n\$config['ads_daugia_link_".$i."'] = '$ads_link';"; 
			$str .= "\n\$config['ads_daugia_img".$i."'] = '$ads_img';"; 
			$i++;             
		endforeach;*/
		
		$str .="/*********Quảng cáo - Khuyen mai**************/\n";  
		/*$listadskm = $this->product->get_ads_by_cat(0); 
		$i=1;
		foreach($listadskm as $ads):
			$ads_name = str_replace("'","\'",$ads->AdsName);
			$ads_link = str_replace("'","\'",$ads->AdsLink);
			$ads_img = $ads->AdsImg;
			$str .= "\n\$config['ads_km_name_".$i."'] = '$ads_name';"; 
			$str .= "\n\$config['ads_km_link_".$i."'] = '$ads_link';"; 
			$str .= "\n\$config['ads_km_img_".$i."'] = '$ads_img';"; 
			$i++; 
		endforeach;*/
						
		foreach($listcat_cap1 as $cap1):
			/*$listads = $this->product->get_ads_by_cat($cap1->CategoryID);
			$str .="\n/*********Quảng cáo************** /\n";
			$i=1;
			foreach($listads as $ads):
				$ads_name = str_replace("'","\'",$ads->AdsName);
				$ads_link = str_replace("'","\'",$ads->AdsLink);
				$ads_img = $ads->AdsImg;
				$str .= "\n\$config['ads_name_".$i."_".$cap1->CategoryID."'] = '$ads_name';"; 
				$str .= "\n\$config['ads_link_".$i."_".$cap1->CategoryID."'] = '$ads_link';"; 
				$str .= "\n\$config['ads_img_".$i."_".$cap1->CategoryID."'] = '$ads_img';"; 
				$i++; 
			endforeach;*/            
			$str .="/*********Cấp 1_".$c1."**************/";
			$cap1_id = $cap1->CategoryID;
			$cap1_name = str_replace("'","\'",$cap1->CategoryName);
			$cap1_alias = str_replace("'","\'",$cap1->CategoryAlias);
			$str .= "\n\$config['main_id_leve_1_$c1'] = $cap1_id;";   
			$str .= "\n\$config['main_name_leve_1_$c1'] = '$cap1_name';";   
			$str .= "\n\$config['main_alias_leve_1_$c1'] = '$cap1_alias';";   
			
			$listcat_cap2 = $this->product->check_parentid($cap1->CategoryID);
			$total_cap2 = count($listcat_cap2);
			$str .= "\n\$config['main_total_leve_1_$c1'] = $total_cap2;";
			if($total_cap2 > 0){
				$c2 = 1;
				foreach($listcat_cap2 as $cap2):
					$str .= "\n";
					$str .="\n/*********Cấp 2_".$c1."_".$c2."**************/";
					$cap2_id = $cap2->CategoryID;
					$cap2_name = str_replace("'","\'",$cap2->CategoryName);
					$cap2_alias = str_replace("'","\'",$cap2->CategoryAlias);
					$str .= "\n\$config['main_id_leve_2_".$c1."_".$c2."'] = $cap2_id;";   
					$str .= "\n\$config['main_name_leve_2_".$c1."_".$c2."'] = '$cap2_name';"; 
					$str .= "\n\$config['main_alias_leve_2_".$c1."_".$c2."'] = '$cap2_alias';"; 
					   
					$listcat_cap3 = $this->product->check_parentid($cap2->CategoryID);
					$total_cap3 = count($listcat_cap3);
					$str .= "\n\$config['main_total_leve_2_".$c1."_".$c2."'] = $total_cap3;";
					if($total_cap3 > 0){
						$c3 = 1;
						foreach($listcat_cap3 as $cap3):
							$str .= "\n";
							$str .="\n/*********Cấp 3_".$c1."_".$c2."_".$c3."**************/";                            
							$cap3_id = $cap3->CategoryID;
							$cap3_name = str_replace("'","\'",$cap3->CategoryName);
							$cap3_alias = str_replace("'","\'",$cap3->CategoryAlias);
							$str .= "\n\$config['main_id_leve_3_".$c1."_".$c2."_".$c3."'] = $cap3_id;";   
							$str .= "\n\$config['main_name_leve_3_".$c1."_".$c2."_".$c3."'] = '$cap3_name';";    
							$str .= "\n\$config['main_alias_leve_3_".$c1."_".$c2."_".$c3."'] = '$cap3_alias';";    
							
							// start level 4
							$listcat_cap4 = $this->product->check_parentid($cap3->CategoryID);
							$total_cap4 = count($listcat_cap4);
							$str .= "\n\$config['main_total_leve_2_".$c1."_".$c2."_".$c3."'] = $total_cap3;";
							if($total_cap4 > 0){
								$c4 = 1;
								foreach($listcat_cap4 as $cap4):
									$str .= "\n";
									$str .="\n/*********Cấp 4_".$c1."_".$c2."_".$c3."_".$c4."**************/";                            
									$cap4_id = $cap4->CategoryID;
									$cap4_name = str_replace("'","\'",$cap4->CategoryName);
									$cap4_alias = str_replace("'","\'",$cap4->CategoryAlias);
									$str .= "\n\$config['main_id_leve_4_".$c1."_".$c2."_".$c3."_".$c4."'] = $cap4_id;";   
									$str .= "\n\$config['main_name_leve_4_".$c1."_".$c2."_".$c3."_".$c4."'] = '$cap4_name';";    
									$str .= "\n\$config['main_alias_leve_4_".$c1."_".$c2."_".$c3."_".$c4."'] = '$cap4_alias';";    
									
								$c4++;                   
								endforeach;  
							}
							$str .= "\n";
							// end level 4
							
						$c3++;                   
						endforeach;  
					}
					$str .= "\n";                     
				$c2++;                   
				endforeach;  
			}
			$str .= "\n";
			$str .= "\n";
		$c1++;
		endforeach;
		
		$str .= "\n\n/* End of file config_ismainmenu quagiare.com*/\n/* Location: ./cf/config_ismainmenu.php */";        
		write_file('../cf/config_ismainmenu.php', $str);          
	}
	
	function addcat()
	{
		$data['title'] = 'Thêm mới danh mục';
		$data['listmaincat'] = $this->product->getMaincat();
		if(!empty($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('CategoryName','Tên danh mục','trim|required');  
			if($this->form_validation->run() == FALSE)
			{
				$this->pre_message = validation_errors();
			}else{
				if($this->product->saveCat()){
					$this->session->set_flashdata('message','Lưu thành công');
					redirect('product/listcat');
				}else{
					$this->pre_message = 'Lưu không thành công';
				}
			} 
		}
		$data['message'] = $this->pre_message;          
		$this->_templates['page'] = 'product/cat/addcat';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function editcat()
	{  
		$CategoryID = $this->uri->segment(3);
		$data['rs'] = $this->product->get_edit_cat($CategoryID);
		$data['title'] = 'Cập nhật danh mục: '.$data['rs']->CategoryName;
		$data['listmaincat'] = $this->product->getMaincat();          
		
		if(!empty($_POST['bt_submit'])){
			$this->form_validation->set_rules('CategoryName','Tên danh mục','trim|required');  
			if($this->form_validation->run() == FALSE){
				$this->pre_message = validation_errors();
			}else{
				 if($this->product->saveCat()){
					 $this->session->set_flashdata('message','Lưu thành công');
					 redirect('product/listcat');
				 }else{
					 $this->pre_message = 'Lưu không thành công';
				 }
			} 
		}
		$data['message'] = $this->pre_message;          
		$this->_templates['page'] = 'product/cat/editcat';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function get_level()
	{
		$category = $this->input->post('catid') ;
		$rs = $this->product->get_edit_cat($category) ;
		echo $rs->Level ;
	}
	
	// Xoa tang pham
	function del_gifts_item()
	{
	  $gifts_id = $this->input->post('giftsid');
	  $this->db->where('GiftsID',$gifts_id);
	  if($this->db->delete('productgifts')){
		  $data['msg'] = "Xóa thành công";
	  }else{
		  $data['msg'] = "Xóa không thành công";
	  }
	  echo json_encode($data);
	}
	
	function delscat()
	{
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->product->delcat($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}
		redirect('product/listcat/'.$page);          
	}
	
	function delcat()
	{
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		if($this->product->delcat($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
		redirect('product/listcat/'.$page);          
	}
	  
	// move a category up
	function cat_up()
	{
		$nwc_id = $this->uri->segment(3);
		if(is_numeric($nwc_id)){
			if($this->product->move_up($nwc_id)){
				$this->session->set_flashdata('message','Sắp xếp thành công');
			}else{
				$this->session->set_flashdata('message','Sắp xếp không thành công');
			}
		}
		
		redirect('product/listcat');
	}
	
	// move a category down
	function cat_down()
	{
		$nwc_id = $this->uri->segment(3);
		if(is_numeric($nwc_id)){
			if($this->product->move_down($nwc_id)){
				$this->session->set_flashdata('message','Sắp xếp thành công');    
			}else{
				$this->session->set_flashdata('message','Sắp xếp không thành công');
			} 
		}
		redirect('product/listcat');
	}      
      
	/**********Product*************/
	function listproduct()
	{
		$data['title'] = 'Danh sách sản phẩm';
		$cid = (int)$this->uri->segment(3);
		$data['listcat'] = $this->product->getMaincat();
		$data['add'] = 'product/addproduct';
		//$data['export'] = 'product/exportExcel/'.$cid ;
		
		$config['base_url'] = base_url().'product/listproduct/'.$cid;  
		$config['total_rows']   =  $this->product->getNumProduct($cid);
		$data['num'] = $config['total_rows'];
		$config['per_page']  =   30;
		$config['uri_segment'] = 4; 
		$ord = (int) $this->input->post('orderby') ;
		$this->pagination->initialize($config);
		$data['list'] = $this->product->getAllProduct($cid,$config['per_page'],$this->uri->segment('4'), $ord);
		$data['pagination']    = $this->pagination->create_links();           
		$this->_templates['page'] = 'product/pro/listproduct'; 
		$this->site->load($this->_templates['page'],$data);          
	}
	
	function addproduct()
	{
		$data['title'] = 'Thêm mới sản phẩm';
		$data['listcat'] = $this->product->check_parentid(0);
		//$data['listimg'] = $this->product->get_img_by_session($this->session->userdata('session_id'));
		//$data['menufacture'] = $this->product->getAllMenufacture();
		
		$this->form_validation->set_rules('ProductName','Tên sản phẩm','trim|required');
		$this->form_validation->set_rules('Price','Giá','trim|required');
		
		
		if($this->form_validation->run())
		{
			if($this->product->saveProduct())
			{
				$proImage = $this->input->post('ContentImg');
				if($proImage!=''){
					if(file_exists('../uploads/product_200x200/'.$proImage)){
						unlink('../uploads/product_200x200/'.$proImage);
					}
					fn_resize_image('../uploads/images/'.$proImage,'../uploads/product_200x200/'.$proImage,200,200);
				}
				$this->session->set_flashdata('message','Lưu thành công');
				redirect('product/listproduct');
			}else{
				$this->pre_message ="Lưu không thành công";
			}
		}    
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'product/pro/addproduct';
		$this->site->load($this->_templates['page'],$data);
	}
	
	function editproduct()
	{
		$ProductID = $this->uri->segment(3);
		$data['listmaincat'] = $this->product->check_parentid(0);
		$data['rs'] = $this->product->get_info_product($ProductID);
		
		$data['title'] = 'Cập nhật sản phẩm '.$data['rs']->ProductName;
		
		$this->form_validation->set_rules('ProductName','Tên sản phẩm','trim|required');
		$this->form_validation->set_rules('Price','Giá cty','trim|required');
		
		if($this->form_validation->run())
		{
			if($this->product->saveProduct())
			{
				$proImage = $this->input->post('ContentImg');
				if($proImage!=''){
					if(file_exists('../uploads/product_200x200/'.$proImage)){
						unlink('../uploads/product_200x200/'.$proImage);
					}
					fn_resize_image('../uploads/images/'.$proImage,'../uploads/product_200x200/'.$proImage,200,200);
				}
				$this->session->set_flashdata('message','Lưu thành công');
				redirect('product/listproduct');
			}else{
				$this->pre_message ="Lưu không thành công";
			}
		}    
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'product/pro/editproduct';
		$this->site->load($this->_templates['page'],$data);
	} 
	
	function delsproduct()
	{
		$page = (int)$this->input->post('page');
		$catid = (int)$this->input->post('catid');
	  
		if(isset($_POST['btn_submitHN']) && ($_POST['btn_submitHN']=='OrderHN'))
		{
			$pid = $this->input->post('ar_pid');
			$ar_pid = explode(',',$pid);
			$num_pid =  count($ar_pid);
			for($i=0;$i<$num_pid;$i++){
				$Ordering = (int)$this->input->post('OrderHN'.$ar_pid[$i]);
				if($Ordering!=0)
				{
					$data = array(
						'OrderHN' => $Ordering
					);
					$this->db->where('ProductID',$ar_pid[$i]);
					if($this->db->update('product',$data)){
						$this->session->set_flashdata('message','Sắp xếp Sản phẩm HN thành công');
					}else{
						$this->session->set_flashdata('message','Sắp xếp Sản phẩm HN không thành công');
					}
				}
			}
		}
	  if(isset($_POST['btn_submit']) && ($_POST['btn_submit']=='Lưu'))
	  {
		$pid = $this->input->post('ar_pid');
		$ar_pid = explode(',',$pid);
		$num_pid =  count($ar_pid);
		for($i=0;$i<$num_pid;$i++){
			$Ordering = (int)$this->input->post('Order'.$ar_pid[$i]);
			if($Ordering!=0)
			{
				$data = array(
					'Ordering' => $Ordering
				);
				$this->db->where('ProductID',$ar_pid[$i]);
				if($this->db->update('product',$data)){
					$this->session->set_flashdata('message','Sắp xếp thành công');
				}else{
					$this->session->set_flashdata('message','Sắp xếp không thành công');
				}
			}
		}
		
	  }else{
		if(!empty($_POST['ar_id']))
		{
	
			$ar_id = $this->input->post('ar_id');
			
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) {
					if ($ar_id[$i]){
						if($this->product->delproduct($ar_id[$i]))
							$this->session->set_flashdata('message','Đã xóa thành công');
						else $this->session->set_flashdata('message','Xóa không thành công');
					}
				}
			}
		}              
	  }
	  redirect('product/listproduct/'.$catid.'/'.$page); 
	
	}
	
	function delproduct()
	{
		$id = $this->uri->segment(3);
		$catid = $this->uri->segment(4);
		$page = $this->uri->segment(5);
		if($this->product->delproduct($id))
			$this->session->set_flashdata('message','Đã xóa thành công');
		else $this->session->set_flashdata('message','Xóa không thành công');
		redirect('product/listproduct/'.$page);          
	}  
	
	function search()
	{
		$data['title'] = 'Kết quả tìm kiếm';
		$data['add'] = 'product/addproduct';
		
		//xuất file excel
		$excel =  $this->uri->segment(3) ;
		if($excel=='export')
		{
			$cid = (int)$this->uri->segment(4) ;
			
			$list = $this->product->getAllProductExport($cid);
			ini_set('memory_limit', '100M');
			memory_get_peak_usage(true);
			require_once APPEXCEL.'excel/PHPExcel.php';
			
		   // Create new PHPExcel object		   
		   $this->phpexcel = new PHPExcel(); 
		   // Set properties
			$this->phpexcel->getProperties()->setCreator("quagiare.com")
										 ->setLastModifiedBy("quagiare.com")
										 ->setTitle("Thong ke san pham quang cao")
										 ->setSubject("Thong ke san pham quang cao")
										 ->setDescription("File thong ke san pham quang cao. Author: Tran Ngoc Duoc. Website:http://quagiare.com.")	//http://360vnit.com
										 ->setKeywords("office 2007 openxml php")
										 ->setCategory("Thong ke san pham quang cao");
			
			$this->phpexcel->getDefaultStyle()->getFont()->setName('Arial');
			$this->phpexcel->getDefaultStyle()->getFont()->setSize(10);            
			// SET HEADER FILE EXCEL
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('A1', 'Thống kê giá sản phẩm ngành.');          
			//Pro ID
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('A3', 'Product ID');
			$this->phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth(15); 
			//Pro Code
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('B3', 'Ngành nghề'); //ProductCode
			$this->phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth(25); 
			// Pro Name
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('C3', 'Product Name');
			$this->phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(80);
			// Prie
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('D3', 'Price');
			$this->phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
			// Price khuyen mai
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('E3', 'Price promotion');
			$this->phpexcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
			// Giam gia
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('F3', 'Discount');
			$this->phpexcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
			                      
			$i=4;
			$k=1;
			foreach($list as $rs):
				//Pro ID
				$this->phpexcel->setActiveSheetIndex(0)->setCellValue('A'.$i, $rs->ProductID);
				//Pro Code
				$this->phpexcel->setActiveSheetIndex(0)->setCellValue('B'.$i, $this->product->get_edit_cat($rs->CategoryID)->CategoryName); //$rs->ProductCode
				// ProductName
				$this->phpexcel->setActiveSheetIndex(0)->setCellValue('C'.$i, $rs->ProductName);
				// Price thi truong
				$this->phpexcel->setActiveSheetIndex(0)->setCellValue('D'.$i, $rs->Price);
				// khuyen mai
				$Pricepromo = !empty($rs->PriceCty) ? $rs->PriceCty : $rs->Price ;
				$this->phpexcel->setActiveSheetIndex(0)->setCellValue('E'.$i, $Pricepromo);
				// Giam gia
				$discount = !empty($rs->PriceCty) ? number_format($rs->Price - $rs->PriceCty,0,',','.') : 0 ;
				$this->phpexcel->setActiveSheetIndex(0)->setCellValue('F'.$i, $discount);
				         
				$i++;
				$k++;
			endforeach;
			//set Font
			$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
			$this->phpexcel->getActiveSheet()->getStyle('A2')->getFont()->setBold(true);
	
			// Witdh
			$this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
			$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setName('Tahoma');
			$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14); 
			$this->phpexcel->getActiveSheet()->getStyle('A2')->getFont()->setName('Tahoma');
			$this->phpexcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);                
			// Rename sheet
			 $this->phpexcel->getActiveSheet()->setTitle('Banggia');
	
			 
			// Set active sheet index to the first sheet, so Excel opens this as the first sheet
			 $this->phpexcel->setActiveSheetIndex(0);
	
			$date = date('d_m_Y');
			
			// Redirect output to a client’s web browser (Excel5)
			header('Content-Type: application/vnd.ms-excel');
			header('Content-Disposition: attachment;filename="Banggia'.$date.'.xls"');
			header('Cache-Control: max-age=0');
	
			$objWriter = PHPExcel_IOFactory::createWriter( $this->phpexcel, 'Excel5');
			$objWriter->save('php://output');
			exit;    
		}
		
		$cid = empty($_POST['cid']) ? (int)$this->uri->segment(3) : (int)$_POST['cid'];
		$data['export'] = 'product/export/'.$cid ;
		//$data['export'] = 'product/search/export/'.$cid;
		
		$this->session->keep_flashdata('keyword');
		$ss_flash_keyword = $this->session->flashdata('keyword');
		$keyword = empty($_POST['keyword']) ? $ss_flash_keyword : $_POST['keyword'];
		//$ar_cid = $this->product->get_array_cat_search($cid);
		
		while(strpos($keyword, ' ')!==false)
		{
			$keyword = str_replace('  ', ' ', $keyword);
			$keyword = trim($keyword);
		}
		$this->session->set_flashdata('keyword', $keyword);
		$data['listcat'] = $this->product->getMaincat();
		$config['base_url'] = base_url().'product/search/'.$cid;  
		
		$config['total_rows']   =  $this->product->getNumProductSea($cid,$keyword); 
		$data['num'] = $config['total_rows'];
		$config['per_page']  = 25 ; 
		$config['uri_segment'] = 4; 
		$this->pagination->initialize($config);
		$ord = (int) $this->input->post('orderby') ;
		$data['list'] =   $this->product->getAllProductSea($cid,$keyword,$config['per_page'],$this->uri->segment('4'),$ord);
		$data['pagination']    = $this->pagination->create_links();          
		$keyword = $this->db->escape_str($keyword);
		$this->_templates['page'] = 'product/pro/search';
		$this->site->load($this->_templates['page'],$data);        
	} 
	
	function export()
	{
		
		$cid = (int)$this->uri->segment(3) ;
		
		$list = $this->product->getAllProductExport($cid);
		ini_set('memory_limit', '100M');
		memory_get_peak_usage(true);
		require_once APPEXCEL.'excel/PHPExcel.php';
		
		// Create new PHPExcel object		   
		$this->phpexcel = new PHPExcel(); 
		// Set properties
		$this->phpexcel->getProperties()->setCreator("quagiare.com")
									 ->setLastModifiedBy("quagiare.com")
									 ->setTitle("Thong ke san pham quang cao")
									 ->setSubject("Thong ke san pham quang cao")
									 ->setDescription("File thong ke san pham quang cao. Author: Tran Ngoc Duoc. Website:http://quagiare.com.")
									 ->setKeywords("office 2007 openxml php")
									 ->setCategory("Thong ke san pham quang cao");
		
		$this->phpexcel->getDefaultStyle()->getFont()->setName('Arial');
		$this->phpexcel->getDefaultStyle()->getFont()->setSize(10);            
		// SET HEADER FILE EXCEL
		//$this->phpexcel->setActiveSheetIndex(0)->setCellValue('A1', 'Thống kê giá sản phẩm ngành.');          
		//Pro ID
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('A3', 'Product ID');
		$this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); 
		//Pro Code
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('B3', 'Ngành nghề');
		$this->phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
		//Pro Code
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('C3', 'Hình ảnh');
		$this->phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); 
		// Pro Name
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('D3', 'Product Name');
		$this->phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
		// Prie
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('E3', 'Giá miền nam');
		$this->phpexcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
		// Price khuyen mai
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('F3', 'Giá miền bắc');
		$this->phpexcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
		// Giam gia
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('G3', 'Khuyến mãi miền nam');
		$this->phpexcel->getActiveSheet()->getColumnDimension('G')->setWidth(25);
		// Giam gia
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('H3', 'Khuyến mãi Hà Nội');
		$this->phpexcel->getActiveSheet()->getColumnDimension('H')->setWidth(25);
		$i=4;
		$k=1;
		foreach($list as $rs):
			//Pro ID
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('A'.$i, $rs->ProductID);
			//Pro Code
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('B'.$i, $this->product->get_edit_cat($rs->CategoryID)->CategoryName);
			// Product Image
			$objDrawing = new PHPExcel_Worksheet_Drawing();
			$objDrawing->setName($rs->ProductName);
			$objDrawing->setDescription($rs->ProductName);
			$objDrawing->setPath('../uploads/images/'.$rs->ProductImg);
			$objDrawing->setCoordinates('C'.$i);
			$objDrawing->setOffsetX(5);
			$objDrawing->setRotation(100);
			$objDrawing->setHeight(20);
			$objDrawing->getShadow()->setVisible(true);
			$objDrawing->getShadow()->setDirection(45);
			$objDrawing->setWorksheet($this->phpexcel->getActiveSheet());
			//$this->phpexcel->setActiveSheetIndex(0)->setCellValue('C'.$i, 'http://quagiare.com/uploads/images/'.$rs->ProductImg);
			// Product Name
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('D'.$i, $rs->ProductName);
			// Giá miền nam
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('E'.$i, $rs->Price);
			// giá miền bắc
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('F'.$i, $rs->PriceHN);
			// Giam giá miền nam
			//$PriceCty = !empty($rs->PriceCty) ? $rs->PriceCty : $rs->Price ;
			$discount = !empty($rs->PriceCty) ? number_format($rs->Price - $rs->PriceCty,0,'','') : 0 ;
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('G'.$i, $discount);
			//Giảm giá hà nội
			//$PriceHN = !empty($rs->PriceHNPromo) ? $rs->PriceHNPromo : $rs->PriceHN ;
			$discountHN = !empty($rs->PriceHNPromo) ? number_format($rs->PriceHN - $rs->PriceHNPromo,0,'','') : 0 ;
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('H'.$i, $discountHN);
						
			$i++;
			$k++;
		endforeach;
		
		//set Font
		$this->phpexcel->getActiveSheet()->getStyle('A3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('B3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('C3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('D3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('E3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('F3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('G3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('H3')->getFont()->setBold(true);

		// Witdh
		//$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setName('Tahoma');
		//$this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		//$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14); 
		//$this->phpexcel->getActiveSheet()->getStyle('A2')->getFont()->setName('Tahoma');
		//$this->phpexcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);                
		
		// Rename sheet
		$this->phpexcel->getActiveSheet()->setTitle('Banggia');

		 
		//Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$this->phpexcel->setActiveSheetIndex(0);

		$date = date('d_m_Y');
		
		// Redirect output to a client's web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="Banggia'.$date.'.xls"');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter( $this->phpexcel, 'Excel5');
		$objWriter->save('php://output');
		exit;    
	}
	
	function exportExcel($cid)
	{
		
		$cid = (int)$this->uri->segment(3) ;
		
		$list = $this->product->getAllProduct($cid,500,0);
		ini_set('memory_limit', '100M');
		memory_get_peak_usage(true);
		require_once APPEXCEL.'excel/PHPExcel.php';
		
		// Create new PHPExcel object		   
		$this->phpexcel = new PHPExcel(); 
		// Set properties
		$this->phpexcel->getProperties()->setCreator("quagiare.com")
									 ->setLastModifiedBy("quagiare.com")
									 ->setTitle("Thong ke san pham quang cao")
									 ->setSubject("Thong ke san pham quang cao")
									 ->setDescription("File thong ke san pham quang cao. Author: Tran Ngoc Duoc. Website:http://quagiare.com.")
									 ->setKeywords("office 2007 openxml php")
									 ->setCategory("Thong ke san pham quang cao");
		
		$this->phpexcel->getDefaultStyle()->getFont()->setName('Arial');
		$this->phpexcel->getDefaultStyle()->getFont()->setSize(10);            
		// SET HEADER FILE EXCEL
		//$this->phpexcel->setActiveSheetIndex(0)->setCellValue('A1', 'Thống kê giá sản phẩm ngành.');          
		//Pro ID
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('A3', 'Product ID');
		$this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(20); 
		//Pro Code
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('B3', 'Ngành nghề');
		$this->phpexcel->getActiveSheet()->getColumnDimension('B')->setWidth(35);
		//Pro Code
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('C3', 'Hình ảnh');
		$this->phpexcel->getActiveSheet()->getColumnDimension('C')->setWidth(20); 
		// Pro Name
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('D3', 'Product Name');
		$this->phpexcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
		// Prie
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('E3', 'Giá miền nam');
		$this->phpexcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
		// Price khuyen mai
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('F3', 'Giá miền bắc');
		$this->phpexcel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
		// Giam gia
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('G3', 'Khuyến mãi miền nam');
		$this->phpexcel->getActiveSheet()->getColumnDimension('G')->setWidth(25);
		// Giam gia
		$this->phpexcel->setActiveSheetIndex(0)->setCellValue('H3', 'Khuyến mãi Hà Nội');
		$this->phpexcel->getActiveSheet()->getColumnDimension('H')->setWidth(25);
		$i=4;
		$k=1;
		foreach($list as $rs):
			//Pro ID
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('A'.$i, $rs->ProductID);
			//Pro Code
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('B'.$i, $this->product->get_edit_cat($rs->CategoryID)->CategoryName);
			// Product Image
			if(!empty($rs->ProductImg))
			{
				$objDrawing = new PHPExcel_Worksheet_Drawing();
				$objDrawing->setName($rs->ProductName);
				$objDrawing->setDescription($rs->ProductName);
				$objDrawing->setPath('../uploads/images/'.$rs->ProductImg);
				$objDrawing->setCoordinates('C'.$i);
				$objDrawing->setOffsetX(5);
				$objDrawing->setRotation(100);
				$objDrawing->setHeight(20);
				$objDrawing->getShadow()->setVisible(true);
				$objDrawing->getShadow()->setDirection(45);
				$objDrawing->setWorksheet($this->phpexcel->getActiveSheet());
				//$this->phpexcel->setActiveSheetIndex(0)->setCellValue('C'.$i, 'http://quagiare.com/uploads/images/'.$rs->ProductImg);
			}
			// Product Name
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('D'.$i, $rs->ProductName);
			// Giá miền nam
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('E'.$i, $rs->Price);
			// giá miền bắc
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('F'.$i, $rs->PriceHN);
			// Giam giá miền nam
			//$PriceCty = !empty($rs->PriceCty) ? $rs->PriceCty : $rs->Price ;
			$discount = !empty($rs->PriceCty) ? number_format($rs->Price - $rs->PriceCty,0,'','') : 0 ;
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('G'.$i, $discount);
			//Giảm giá hà nội
			//$PriceHN = !empty($rs->PriceHNPromo) ? $rs->PriceHNPromo : $rs->PriceHN ;
			$discountHN = !empty($rs->PriceHNPromo) ? number_format($rs->PriceHN - $rs->PriceHNPromo,0,'','') : 0 ;
			$this->phpexcel->setActiveSheetIndex(0)->setCellValue('H'.$i, $discountHN);
						
			$i++;
			$k++;
		endforeach;
		
		//set Font
		$this->phpexcel->getActiveSheet()->getStyle('A3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('B3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('C3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('D3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('E3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('F3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('G3')->getFont()->setBold(true);
		$this->phpexcel->getActiveSheet()->getStyle('H3')->getFont()->setBold(true);

		// Witdh
		//$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setName('Tahoma');
		//$this->phpexcel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		//$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		//$this->phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14); 
		//$this->phpexcel->getActiveSheet()->getStyle('A2')->getFont()->setName('Tahoma');
		//$this->phpexcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(14);                
		
		// Rename sheet
		$this->phpexcel->getActiveSheet()->setTitle('Banggia');

		 
		//Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$this->phpexcel->setActiveSheetIndex(0);

		$date = date('d_m_Y');
		
		// Redirect output to a client's web browser (Excel5)
		header('Content-Type: application/vnd.ms-excel');
		header('Content-Disposition: attachment;filename="Banggia'.$date.'.xls"');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter( $this->phpexcel, 'Excel5');
		$objWriter->save('php://output');
		exit;    
	}
	
	function add_img_product()
	{
		$session_info = $this->session->userdata('session_id');
		$path = $this->input->post('path');
		$ProductID = (int)$this->input->post('ProductID');
		if($this->product->add_img($path,$ProductID,$session_info)){
			echo $this->_load_img_product($ProductID,$session_info);
		}else{
			echo 'Error';
		}
	}
	
	function _load_img_product($ProductID,$sesssion)
	{
	  $list = $this->product->getListImg($ProductID,$sesssion);
	  $data ='<ul class="list_img">';
	  
	  foreach($list as $rs):
		$data .='<li><div  align="center"><img src="'.base_url_site().'uploads/images/'.$rs->ImagePath.'" onclick="chosen('.$rs->ImageID.')"> <a class="delete_button" id="'.$rs->ImageID.'" href="javascript:;"> Xóa</a><span style="display: none;" id="v'.$rs->ImageID.'">'.$rs->ImagePath.'</span></div>
		
		</li>';
	  endforeach;
	  $data .='</ul>';
	  $data .='
		 <script type="text/javascript">
		$(function() {
		$(".delete_button").click(function() {
		if(confirm("Bạn có chắc chắn muốn xóa hình ảnh này\nĐồng ý chọn: OK\nHủy chọn: Cancel")){
		var id = $(this).attr("id");
		var dataString = \'id=\'+ id ;
		var parent = $(this).parent();
			
		$.ajax({
		   type: "POST",
		   url: "'.base_url().'product/delimg",
		   data: dataString,
		   cache: false,
		   beforeSend: function(){
		   parent.animate({\'backgroundColor\':\'#fb6c6c\'},300).animate({ opacity: 0.35 }, "past");;
		   }, 
		   success: function(){
			parent.slideUp(\'slow\', function() {$(this).remove();});
		  }   
		 });
		 }
		return false;
			});
		});
		function chosen(id)
		{
			if(id)
			{
				var vl = $(\'#v\'+id).text();
				$(\'#ProductImg\').val(vl);
				$(\'#main_img\').html(\'<img src="\' + urlimg + vl + \'" width="100" />\');
			}
			else
			{
				return false;
			}
		}            
		</script>          
	  ';
	  return $data;
	}
	
	function delimg()
	{
	  $id = $this->input->post('id');
	  $this->db->where('ImageID',$id);
	  $item = $this->db->get('productimage')->row();
	  $images = '../uploads/images/'.$item->ImagePath;
	  $images_30 = '../uploads/product/30/'.$item->ImagePath;
	  $images_40 = '../uploads/product/40/'.$item->ImagePath;
	  $images_80 = '../uploads/product/80/'.$item->ImagePath;
	  $images_200 = '../uploads/product/200/'.$item->ImagePath;
	  if(file_exists($images)){
		  unlink($images);
	  }
	  if(file_exists($images_30)){
		  unlink($images_30);
	  }          
	  if(file_exists($images_40)){
		  unlink($images_40);
	  }
	  if(file_exists($images_80)){
		  unlink($images_80);
	  }
	  if(file_exists($images_200)){
		  unlink($images_200);
	  }                              
	  $this->db->where('ImageID',$id);
	  $this->db->delete('productimage');
	  
	} 
	
	// Promotion 
	function discount()
	{
	  $data['title'] = 'Danh sách sản phẩm khuyến mãi';
	  $cid = (int)$this->uri->segment(3);
	  $data['listcat'] = $this->product->getMaincat();
	  $data['add'] = 'product/addproduct';
	  $config['base_url'] = base_url().'product/discount/'.$cid;  
	  $config['total_rows']   =  $this->product->getNumProduct($cid);
	  $data['num'] = $config['total_rows'];
	  $config['per_page']  =   30;
	  $config['uri_segment'] = 4; 
	  $this->pagination->initialize($config);   
	  $data['list'] =   $this->product->listpromotion($config['per_page'],$this->uri->segment('4'));
	  $data['pagination']    = $this->pagination->create_links();           
	  $this->_templates['page'] = 'discount/listdiscount';
	  $this->site->load($this->_templates['page'],$data);          
	}
}
?>
